package com.lanou.zakerproject.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.zakerproject.subscribe.ChannelSearch;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class DbTool implements DbValue{
    private SQLiteDatabase sqLiteDatabase;//数据库对象
    private Context context;//用于创建数据库和弹出Toast
    private MySqlHelper mySqlHelper;

    public DbTool(Context context) {
        this.context = context;
        mySqlHelper = new MySqlHelper(context, "tag.db", null, 1);
       // 获得一个可写的数据库对象
        sqLiteDatabase = mySqlHelper.getWritableDatabase();
    }

    public void addData(String text){
        ContentValues values = new ContentValues();
        values.put("title", text);
        sqLiteDatabase.insert(TABLE_NAME,null,values);
    }

    public List<String> allData(){
        List<String> data = new ArrayList<>();
        String sql = "select * from "+TABLE_NAME;
        Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            String title = cursor.getString(cursor.getColumnIndex(COLUME_TITLE));
            data.add(title);
        }
        cursor.close();
        return data;
    }

    public void DelData(String title){
        String sql = "delete from "+TABLE_NAME+" where title = ?";
        sqLiteDatabase.execSQL(sql,new String[]{title});
    }


}





